package edu.pucsp.predatorprey.tela;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.pucsp.predatorprey.tela.util.GridBagConstraintsBuilder;

public class PainelTerreno extends JPanel {

	private final TerrenoCanvas canvas;

	private JPanel bottomPanel;

	public PainelTerreno(TerrenoCanvas canvas) {
		this.canvas = canvas;
		this.setLayout(new BorderLayout());
		this.initComponents();
	}

	private void initComponents() {
		this.setBorder(BorderFactory.createTitledBorder("Terreno"));
		this.canvas.setPreferredSize(new Dimension(580, 500));
		this.canvas.setBackground(Color.WHITE);
		this.initBottomPanel();
		this.add(this.canvas, BorderLayout.CENTER);
		this.add(this.bottomPanel, BorderLayout.SOUTH);
		this.setPreferredSize(new Dimension(580, 600));
	}

	private void initBottomPanel() {
		this.bottomPanel = new JPanel(new GridBagLayout());
		this.bottomPanel.setPreferredSize(new Dimension(580, 80));
		JLabel imagem = this.createImageLabel("images/prey/presa.png");
		this.bottomPanel.add(imagem, GridBagConstraintsBuilder.createContraint().grid(0, 0).anchor(GridBagConstraints.WEST).fill(GridBagConstraints.NONE)
				.build());
		this.bottomPanel.add(new JLabel("Coelho"),
				GridBagConstraintsBuilder.createContraint().grid(1, 0).anchor(GridBagConstraints.WEST).fill(GridBagConstraints.NONE).weightx(1.0).build());
		imagem = this.createImageLabel("images/predator/predador.png");
		this.bottomPanel.add(imagem, GridBagConstraintsBuilder.createContraint().grid(0, 1).anchor(GridBagConstraints.WEST).fill(GridBagConstraints.NONE)
				.build());
		this.bottomPanel.add(new JLabel("Raposa"),
				GridBagConstraintsBuilder.createContraint().grid(1, 1).anchor(GridBagConstraints.WEST).fill(GridBagConstraints.NONE).weightx(1.0).build());
	}

	private JLabel createImageLabel(String name) {
		BufferedImage imagem;
		JLabel imagemPresa = null;
		try {
			imagem = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(name));
			imagemPresa = new JLabel(new ImageIcon(imagem));

		} catch (IOException e) {
			e.printStackTrace();
		}
		return imagemPresa;
	}

}
